import { BASE_URL } from '@/config';
import { request } from '@/utils/request';

/**
 * 订单相关接口
 */

// 提交订单
export interface OrderItemRequest {
  productId: number;
  productName?: string;
  productImage?: string;
  quantity: number;
  unitPrice?: number;
  tasteOptions?: string;
}

export interface SubmitOrderRequest {
  customerId: number;
  merchantId: number;
  regionId: number;
  paymentMethod?: number;
  addressSnapshot?: string;
  remark?: string;
  items: OrderItemRequest[];
}

// 提交订单
export function submitOrder(data: SubmitOrderRequest) {
  return request({
    url: `${BASE_URL}/user/orders/add/order`,
    method: 'POST',
    data
  });
}

// 根据用户ID查看订单
export function getOrdersByUser(customerId: string) {
  return request({
    url: `${BASE_URL}/user/orders/by-user/${customerId}`,
    method: 'GET'
  });
}

// 根据状态查看订单
export function getOrdersByStatus(customerId: string, status: string) {
  return request({
    url: `${BASE_URL}/user/orders/by-status/${customerId}/${status}`,
    method: 'GET'
  });
}

// 查看订单详情
export function getOrderDetail(orderId: string) {
  return request({
    url: `${BASE_URL}/user/orders/order/detail/${orderId}`,
    method: 'GET'
  });
}

// 支付
export function payOrder(orderId: string) {
  return request({
    url: `${BASE_URL}/user/orders/${orderId}/pay`,
    method: 'POST'
  });
}

// 订单评价
export interface ReviewRequest {
  customerId?: number;
  merchantId?: number;
  score: number;
  content?: string;
  images?: string;
  isAnonymous?: number;
  replyContent?: string;
  repliedAt?: Date;
}

// 评价
export function submitReview(orderId: string, data: ReviewRequest) {
  return request({
    url: `${BASE_URL}/user/orders/order/review/${orderId}`,
    method: 'POST',
    data
  });
}